package me.zhengjie.modules.fund.service.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import me.zhengjie.utils.DateUtil;

import javax.validation.constraints.Min;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 个人薪资计算参数
 */
@Getter
@Setter
public class ReqSalaryCalculate implements Serializable {
    private static final long serialVersionUID = 4024651924266481587L;

    private List<SocialSecurityConfig> defaultConfig = new ArrayList<>();

    private List<TaxRateTableItem> defaultTaxRateTables = new ArrayList<>();

    private void initDefaultTaxRateTables() {
        defaultTaxRateTables.add(TaxRateTableItem.builder().start(0).end(36000d).rate(0.03).quickDeductionAmount(0d).build());
        defaultTaxRateTables.add(TaxRateTableItem.builder().start(36001).end(144000d).rate(0.1).quickDeductionAmount(2520d).build());
        defaultTaxRateTables.add(TaxRateTableItem.builder().start(144001).end(300000d).rate(0.2).quickDeductionAmount(16920d).build());
        defaultTaxRateTables.add(TaxRateTableItem.builder().start(300001).end(420000d).rate(0.25).quickDeductionAmount(31920d).build());
        defaultTaxRateTables.add(TaxRateTableItem.builder().start(420001).end(660000).rate(0.3).quickDeductionAmount(52920).build());
        defaultTaxRateTables.add(TaxRateTableItem.builder().start(660001).end(960000).rate(0.35).quickDeductionAmount(85920).build());
        defaultTaxRateTables.add(TaxRateTableItem.builder().start(960001).end(1000000000).rate(0.45).quickDeductionAmount(181920).build());
    }

    private void initSocialSecurityConfigs() {
        defaultConfig.add(SocialSecurityConfig.builder().type(SocialSecurityType.Gjj).baseAmount(27786d).rateOfCompany(0.12).rateOfPersonal(0.12).limitAmountOfCompany(3334d).limitAmountOfPersonal(3334d).build());
        defaultConfig.add(SocialSecurityConfig.builder().type(SocialSecurityType.Yanglao).baseAmount(26541d).rateOfCompany(0.16).rateOfPersonal(0.08).limitAmountOfCompany(4246.56).limitAmountOfPersonal(2123.28).build());
        defaultConfig.add(SocialSecurityConfig.builder().type(SocialSecurityType.Yiliao).baseAmount(29732d).rateOfCompany(0.09).rateOfPersonal(0.02).limitAmountOfCompany(2675.88).limitAmountOfPersonal(597.64).fixAmountOfPersonal(3d).build());
        defaultConfig.add(SocialSecurityConfig.builder().type(SocialSecurityType.Shiye).baseAmount(26541d).rateOfCompany(0.008)
                .rateOfPersonal(0.002)
                .limitAmountOfCompany(212.33)
                .limitAmountOfPersonal(53.08)
                .build());
        LocalDate newShiyeDate = LocalDate.of(2021, 5, 1);
        defaultConfig.add(SocialSecurityConfig.builder().type(SocialSecurityType.Shiye).baseAmount(26541d).rateOfCompany(0.005)
                .rateOfPersonal(0.005)
                .limitAmountOfCompany(132.71)
                .limitAmountOfPersonal(132.71)
                .startDate(DateUtil.toDate(newShiyeDate))
                .build());
        defaultConfig.add(SocialSecurityConfig.builder()
                .type(SocialSecurityType.Gongshang)
                .baseAmount(26541d)
                .rateOfCompany(0.004)
                .rateOfPersonal(0d)
                .limitAmountOfCompany(106.16)
                .limitAmountOfPersonal(0d)
                .build());
        defaultConfig.add(SocialSecurityConfig.builder()
                .type(SocialSecurityType.Shengyu)
                .baseAmount(29732d)
                .rateOfCompany(0.008)
                .rateOfPersonal(0d)
                .limitAmountOfCompany(237.86)
                .limitAmountOfPersonal(0d)
                .build());
    }

    public ReqSalaryCalculate() {
        initSocialSecurityConfigs();
        initDefaultTaxRateTables();
    }

    /**
     * 社保计算参数
     */
    private List<SocialSecurityConfig> socialSecurityConfigs = new ArrayList<>();

    /**
     * 薪资月份
     */
    @Min(1)
    private Integer month;

    /**
     * 税前薪资
     */
    @Min(0)
    private Double salaryBeforeTax;

    /**
     * 专项附加扣除金额
     */
    private Double specialAddonAmount = (double) 0;

    /**
     * 个税起征点
     */
    private Double taxThreshold;

    /**
     * 社保基数
     */
    private Double socialSecurityBase;

    /**
     * 医疗基数
     */
    private Double yiliaoBaseAmount;

    /**
     * 公积金基数
     */
    private Double gjjBaseAmount;

    /**
     * 社保基数
     */
    @Setter
    @Getter
    @Builder
    public static class SocialSecurityConfig {

        /**
         * 开始日期
         */
        private Date startDate;

        /**
         * 社保类型
         */
        private SocialSecurityType type;

        /**
         * 社保基数
         */
        private double baseAmount;

        /**
         * 企业上交比例
         */
        private double rateOfCompany;

        /**
         * 企业上交限制
         */
        private double limitAmountOfCompany;

        /**
         * 个人上交比例
         */
        private double rateOfPersonal;

        /**
         * 个人上交限制
         */
        private double limitAmountOfPersonal;

        /**
         * 个人须上交固定金额
         */
        private double fixAmountOfPersonal;
    }

    @Getter
    @Setter
    @Builder
    public static class TaxRateTableItem {
        private double start;

        private double end;

        private double rate;

        private double quickDeductionAmount;
    }
}
